package com.sugou.web.ctrl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sugou.model.commons.Area;
import com.sugou.model.datas.MerchantType;
import com.sugou.model.datas.Merchants;
import com.sugou.model.datas.embeddable.Address;
import com.sugou.model.datas.embeddable.Business;
import com.sugou.model.datas.embeddable.Contacts;
import com.sugou.model.datas.embeddable.Point;
import com.sugou.services.input.InputService;


@Controller
@RequestMapping("/")
@SuppressWarnings({"rawtypes"})
@Scope("prototype")
public class InputCtrl extends WebControllerSupport{
	private static final long serialVersionUID = 6894158180272009436L;

	@Resource(name="inputService")
	private InputService<Serializable> inputService;
	
	@RequestMapping(value={"add"},method={RequestMethod.GET})
	public String add(){
		return "/views/form";
	}
	
	@RequestMapping(value={"save"},method={RequestMethod.POST})
	public String save(){
		Merchants merchants=new Merchants();
		merchants.setName(this.request.getParameter("mch_name"));
		Point point=new Point();
		String[] mp=this.request.getParameter("mch_mapPoint").split(",");
		point.setLng(Double.parseDouble(mp[0]));
		point.setLat(Double.parseDouble(mp[1]));
		merchants.setPoint(point);
		merchants.setType((MerchantType) this.service.getEntity(MerchantType.class, this.request.getParameter("mch_type")));
		Address address=new Address();
		address.setArea((Area) this.service.getEntity(Area.class, this.request.getParameter("mch_area")));
		address.setDetailed(this.request.getParameter("mch_address"));
		address.setZipNo("00");
		Contacts contacts=new Contacts();
		String[] phones=this.request.getParameterValues("mch_phone");
		String[] mobiles=this.request.getParameterValues("mch_mobile");
		
		boolean flag=false;
		StringBuilder phonestr=new StringBuilder();
		for (int i = 0; i < phones.length; i++) {
			if(StringUtils.isBlank(phones[i])){
				continue;
			}
			if(flag){
				phonestr.append(",");
			}else{
				flag=true;
			}
			phonestr.append(phones[i]);
		}
		
		StringBuilder mobilestr=new StringBuilder();
		for (int i = 0; i < mobiles.length; i++) {
			if(StringUtils.isBlank(mobiles[i])){
				continue;
			}
			if(flag){
				mobilestr.append(",");
			}else{
				flag=true;
			}
			mobilestr.append(mobiles[i]);
		}
		
		contacts.setMobiles(mobilestr.toString());
		contacts.setPhones(phonestr.toString());
		
		merchants.setContacts(contacts);
		Business business=new Business();
		business.setServerArea(this.request.getParameter("mch_serverArea"));
		business.setOperatorTime(this.request.getParameter("mch_operatorTime"));
		business.setResponseTime(Integer.valueOf(this.request.getParameter("mch_responseTime")));
		business.setLowest(Integer.valueOf(this.request.getParameter("mch_lowest")));
		merchants.setBusiness(business);
		merchants.setAddress(address);
		String[] rasarray=this.getParam("mch_ras").split(",");
		
		String[] mch_com_names=this.getParam("mch_com_names").split(",");
		
		List<Map<String, String>> comm=new ArrayList<Map<String,String>>();
		for (int i = 0; i < mch_com_names.length; i++) {
			if(StringUtils.isBlank(mch_com_names[i])){
				continue;
			}
			Map<String, String> item=new HashMap<String, String>();
			item.put("name", mch_com_names[i]);
			item.put("price", "0");
			comm.add(item);
		}
		
		this.inputService.saveMer(merchants,new ArrayList<String>(new HashSet<String>(Arrays.asList(rasarray))), comm);
		return "redirect:/list";
	}

	public InputService<Serializable> getInputService() {
		return inputService;
	}

	public void setInputService(InputService<Serializable> inputService) {
		this.inputService = inputService;
	}
}
